#!/bin/sh
#
# Test sh scripts
#
# Copyright 2019 by STMicroelectronics
# Licensed under GPLv2, see file LICENSE in this source tree.

. ./testing.sh

test -f "$bindir/.config" && . "$bindir/.config"

# testing "test name" "options" "expected result" "file input" "stdin"

# Test case
testing "shebang" \
	"uudecode; sh -c './shebang.sh'; echo \$?" \
	"Hello world
0
" \
"" "\
begin-base64 755 shebang.sh
IyEvYmluL3NoCmVjaG8gIkhlbGxvIHdvcmxkIgo=
====
"
rm -f shebang.sh

# Test case
testing "shebang with whitespace" \
	"uudecode; sh -c './shebang_trailing_space.sh'; echo \$?" \
	"Hello world
0
" \
"" "\
begin-base64 755 shebang_trailing_space.sh
IyEvYmluL3NoIAplY2hvICJIZWxsbyB3b3JsZCIK
====
"
rm -f shebang_trailing_space.sh

# Test case
testing "shebang with argument" \
	"uudecode; sh -c './shebang_argument.sh'; echo \$?" \
	"Hello world
0
" \
"" "\
begin-base64 755 shebang_argument.sh
IyEvYmluL3NoIC0KZWNobyAiSGVsbG8gd29ybGQiCg==
====
"
rm -f shebang_argument.sh

# Test case
testing "shebang with leading whitespace and argument" \
       "uudecode; sh -c './shebang_leading_space_argument.sh'; echo \$?" \
       "Hello world
0
" \
"" "\
begin-base64 755 shebang_leading_space_argument.sh
IyEvYmluL3NoICAtCmVjaG8gIkhlbGxvIHdvcmxkIgo=
====
"
rm -f shebang_leading_space_argument.sh

# Test case
testing "shebang with argument and trailing whitespace" \
	"uudecode; sh -c './shebang_argument_trailing_space.sh'; echo \$?" \
	"Hello world
0
" \
"" "\
begin-base64 755 shebang_argument_trailing_space.sh
IyEvYmluL3NoIC0gCmVjaG8gIkhlbGxvIHdvcmxkIgo=
====
"
rm -f shebang_argument_trailing_space.sh

# Test case
testing "shebang with leading whitespace, argument and trailing whitespace" \
       "uudecode; sh -c './shebang_leading_argument_trailing_space.sh'; echo \$?" \
       "Hello world
0
" \
"" "\
begin-base64 755 shebang_leading_argument_trailing_space.sh
IyEvYmluL3NoICAtIAplY2hvICJIZWxsbyB3b3JsZCIK
====
"
rm -f shebang_leading_argument_trailing_space.sh

case "$COMSPEC" in
*\\cmd.exe)
	testing "'cmd //c' works" \
	"cmd //c dir /b /w sh.tests" \
	"sh.tests\r
" \
	"" \
	""
	;;
esac

testing "CR in backticks is only eaten if preceding a NL" \
	"cr=\"\$(printf \\\\015)\"; printf %s \"\$cr\" | hexdump -C" \
	"00000000  0d                                                |.|
00000001
" \
	"" \
	""

exit $FAILCOUNT
